Hệ thống quản lý trường học bằng PHP / MySQLi
1 <?php
2 include('connect.php');
3
4 if(isset($_POST["Importcourse"])){
5
6 $filename=$_FILES["file"]["tmp_name"];
7
8
9 if($_FILES["file"]["size"] > 0)
10 {
11 $file = fopen($filename, "r");
12 while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
13 {
14
15
16 $sql = "INSERT into course (
17 department_id,
18 coursename,
19 exambody,
20 duration,
21 feepayable)
22 values (
23 '".$getData[1]."',
24 '".$getData[2]."',
25 '".$getData[3]."',
26 '".$getData[4]."',
27 '".$getData[5]."'
28 )";
29 $result = mysqli_query($db, $sql);
30 if(!isset($result))
31 {
32 echo "<script type=\"text/javascript\">
33 alert(\"Invalid File:Please Upload CSV File.\");
34 window.location = \"csvtesting.php\"
35 </script>";
36 }
37 else {
38 echo "<script type=\"text/javascript\">
39 alert(\"CSV File has been successfully Imported.\");
40 window.location = \"csvtesting.php\"
41 </script>";
42 }
43 }
44
45 fclose($file);
46 }
47 }
48 ?>
49
50 <?php
51
52 if(isset($_POST["Exportcourse"])){
53
54 header('Content-Type: text/csv; charset=utf-8');
55 header('Content-Disposition: attachment; filename=data.csv');
56 $output = fopen("php://output", "w");
57 fputcsv($output, array('Course ID', 'Department ID','Class name', 'Exambody', 'Duration', 'Fee Payable'));
58 $query = "SELECT * from course ORDER BY course_id DESC";
59 $result = mysqli_query($db, $query);
60 while($row = mysqli_fetch_assoc($result))
61 {
62 fputcsv($output, $row);
63 }
64 fclose($output);
65 }
66
67 ?>